package com.shen.server;

import com.shen.pojo.MobileCard;
import com.shen.server.serive.CallService;
import com.shen.server.serive.SendService;

/**
 * Author:shen
 * CreateDate:2020/6/11.
 * Function:话痨套餐
 */
public class TalkPackage extends ServicePackage  implements CallService, SendService {
    private int talkTime=500;//通话时长
    private int smsCount=30;//短信条数

    public TalkPackage() {
        this.price=58;
    }

    @Override
    public String showInfo() {
        return super.showInfo();
    }

    @Override
    public int call(int minCount, MobileCard mobileCard) throws Exception {

        int temp=minCount;
        //分两种方式
        for(int i=0;i<minCount;i++){
            //查看是否剩余1分钟
            if(this.talkTime-mobileCard.getRealTalkTime()>=1){
                //循环一次，加一
                mobileCard.setRealTalkTime(mobileCard.getRealTalkTime()+1);
            }else if(mobileCard.getMoney()>=0.2){//是否能支付
                //没有1分钟
                mobileCard.setRealTalkTime(mobileCard.getRealTalkTime()+1);
                //减余额 运行一次就减0.2
                mobileCard.setMoney(mobileCard.getMoney()-0.2);
                //计算消费
                mobileCard.setConsumAmount(mobileCard.getConsumAmount()+0.2);
            }else{
                temp=i;
                //不能支付，也没有
                throw new Exception("本次已通话"+i+"分钟，您的余额不足，请充值后再使用！\n");
            }
        }
        return temp;
    }

    @Override
    public int send(int count, MobileCard mobileCard) throws Exception {
        int temp=count;
        for (int i = 0; i <count ; i++) {
            if(this.smsCount-mobileCard.getRealSMSCount()>=1){
                mobileCard.setRealSMSCount(mobileCard.getRealSMSCount()+1);
            }else if(mobileCard.getMoney()>=0.1){
                mobileCard.setRealSMSCount(mobileCard.getRealSMSCount()+1);
                mobileCard.setMoney(mobileCard.getMoney()-0.1);
                mobileCard.setConsumAmount(mobileCard.getRealTalkTime()+0.1);
            }else{
                temp=i;
                throw new Exception("本次发送"+i+"条短信,您的余额不足，请充值后再使用！\n");
            }
        }
        return temp;
    }

    public TalkPackage(int talkTime, int smsCount, double price) {
        this.talkTime = talkTime;
        this.smsCount = smsCount;
        this.price = price;
    }

    public int getTalkTime() {
        return talkTime;
    }

    public void setTalkTime(int talkTime) {
        this.talkTime = talkTime;
    }

    public int getSmsCount() {
        return smsCount;
    }

    public void setSmsCount(int smsCount) {
        this.smsCount = smsCount;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "话痨套餐：通话时长为"+this.talkTime+"分钟/月，短信条数为"+this.smsCount+"条/月";
    }
}
